package pojo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

import org.hibernate.annotations.GenericGenerator;

/**
 * Created by CHENCO7 on 8/11/2017.
 */
@Entity
public class Customer {
    @Id
    @GenericGenerator(name = "uuid_generator", strategy = "uuid")
    @GeneratedValue(generator = "uuid_generator")
    private String id;
    private String name;
    private String password;
    private String avatar;

    @OneToMany(cascade = {CascadeType.REMOVE})
    @JoinColumn(name = "customer_id")
    private List<Order> order = new ArrayList<Order>();

    @ElementCollection(fetch=FetchType.EAGER)
    @JoinTable(name = "addresses", joinColumns = {@JoinColumn(name = "c_id")})
    private List<Address> addressList = new ArrayList<Address>();

    public Customer() {
    }

    public Customer(String name) {
        this.name = name;
    }

    public Customer(String name, String password) {
		super();
		this.name = name;
		this.password = password;
	}

	public Customer(String id, String name, List<Order> order) {
        this.id = id;
        this.name = name;
        this.order = order;
    }

    public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Order> getOrder() {
        return order;
    }

    public void setOrder(List<Order> order) {
        this.order = order;
    }

    public List<Address> getAddressList() {
        return addressList;
    }

    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }

    public String getDefaultAddress() {
        for (Address address : addressList) {
            if (address.getIsDefaultAddress()) {
                return address.getName();
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", avatar='" + avatar + '\'' +
                ", order=" + order +
                '}';
    }
}
